// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
//
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.designer.core.demos;

import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.SubProgressMonitor;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.dialogs.IOverwriteQuery;
import org.eclipse.ui.wizards.datatransfer.IImportStructureProvider;
import org.eclipse.ui.wizards.datatransfer.ImportOperation;

/**
 * Qiang.Zhang.Adolf@gmail.com class global comment. Detailled comment <br/>
 * 
 * $Id: Dengues.epf 1 2006-09-29 17:06:40Z qiang.zhang $
 * 
 */
public class ImportDemoProjectProvider {

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "importProjectAs".
     * 
     * @param shell
     * @param prjName
     * @param sourcePath
     * @param monitor
     */
    public static void importProjectAs(Shell shell, String prjName, String sourcePath, IProgressMonitor monitor) {
        IImportStructureProvider provider = FilterFileSystemStructureProvider.INSTANCE;
        monitor.beginTask("Importing Demo project", 10);
        File source = new File(sourcePath);
        IPath path = new Path(prjName);
        ArrayList fileSystemObjects = new ArrayList();
        getFilesForProject(fileSystemObjects, provider, source);
        ImportOperation operation = new ImportOperation(path, source, provider, new MyOverwriteQuery(), fileSystemObjects);
        operation.setContext(shell);
        operation.setOverwriteResources(true);
        operation.setCreateContainerStructure(false);
        try {
            monitor.worked(2);
            operation.run(new SubProgressMonitor(monitor, 1));
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            monitor.done();
        }
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "getFilesForProject".
     * 
     * @param files
     * @param provider
     * @param entry
     */
    public static void getFilesForProject(Collection files, IImportStructureProvider provider, Object entry) {
        List children = provider.getChildren(entry);
        Iterator childrenEnum = children.iterator();

        while (childrenEnum.hasNext()) {
            Object child = childrenEnum.next();
            // Add the child, this way we get every files except the project
            // folder itself which we don't want
            files.add(child);
            // We don't have isDirectory for tar so must check for children
            // instead
            if (provider.isFolder(child)) {
                getFilesForProject(files, provider, child);
            }
        }
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com ImportDemoProjectProvider class global comment. Detailled comment <br/>
     * 
     * $Id: Dengues.epf 1 2006-09-29 17:06:40Z qiang.zhang $
     * 
     */
    private static class MyOverwriteQuery implements IOverwriteQuery {

        /*
         * (non-Javadoc)
         * 
         * @see org.eclipse.ui.dialogs.IOverwriteQuery#queryOverwrite(java.lang.String)
         */
        public String queryOverwrite(String pathString) {
            return pathString;
        }

    }
}
